<template>
	<view class="contain">
		<u-navbar :title="detailInfo.workOrderId" @rightClick="rightClick" :fixed="true" :placeholder="true"
			:border="true" height="100upx" :autoBack="true"></u-navbar>
		<view class="card-contain">
			<view class=" card-public">
				<text>店铺号：{{detailInfo.storeId}}</text>
				<text>联系人：{{detailInfo.connectorName}}/{{detailInfo.connectorPhone}}</text>
				<text>店铺地址：{{detailInfo.storeAddress}}</text>
			</view>

			<view class=" card-public">
				<text>{{remarktitle}}：{{detailInfo.remark}}</text>
			</view>

			<view class=" card-public">
				<text>MA：{{detailInfo.maId}}</text>
				<text>故障描述：{{detailInfo.faultDescribe}}</text>
				<text>响应级别：{{detailInfo.responseLevel}}</text>
			</view>

			<view class="card-public">
				<u--input placeholder="设备序列号" v-model="item.serialNumber" border="bottom" clearable></u--input>
				<u--input placeholder="故障设备" v-model="item.wireState" border="bottom" clearable></u--input>

				<view class="textline">
					<u-picker :show="show.show1" :columns="faultClassName" @cancel="show.show1 = false"
						@confirm="confirm($event,1)"></u-picker>
					<text @click="show.show1=true">问题分类：{{faultClassName[0][item.faultClass]}}</text>
				</view>
				<view class="textline">
					<u-picker :show="show.show2" :columns="faultCode" @cancel="show.show2 = false"
						@confirm="confirm($event,2)"></u-picker>
					<text @click="show.show2=true">故障代码：{{faultCode[0][item.faultCode]}}</text>
				</view>
				<view class="textline">
					<u-picker :show="show.show3" :columns="soluteCode" @cancel="show.show3 = false"
						@confirm="confirm($event,3)"></u-picker>
					<text @click="show.show3=true">解决代码：{{soluteCode[0][item.soluteCode]}}</text>
				</view>
				<view class="textline">
					<u-picker :show="show.show4" :columns="repairType" @cancel="show.show4 = false"
						@confirm="confirm($event,4)"></u-picker>
					<text @click="show.show4=true">解决类型：{{item.repairType=="remote"?"远程":"上门"}}</text>
				</view>

			</view>

			<view class=" card-public">
				<u--input placeholder="确认进场时间:例如 2022-10-17 19:00:00" v-model="item.arriveTime" border="bottom"
					clearable></u--input>
				<!-- <view>
				        <u-datetime-picker
				                :show="show"
				                v-model="value1"
				                mode="date"
				        ></u-datetime-picker>
				        <u-button @click="show = true">确认进场时间</u-button>
				    </view> -->
				<u-radio-group class="radio" v-model="item.isGet" placement="row">
					是否收到货物：
					<u-radio label="是" name="true" shape="circle"></u-radio>
					<u-radio label="否" name="false" shape="circle"></u-radio>
				</u-radio-group>
				<u--input placeholder="核对点位时间 例如 2022-10-17 19:00:00" v-model="item.isOptionalDate" border="bottom"
					clearable></u--input>
			</view>

			<view class=" card-public">
				<text>核对点位现场图片：</text>
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					:maxCount="10"></u-upload>
			</view>

			<view class="card-public">
				<u--input placeholder="综合布线时间 例如 2022-10-17 19:00:00" v-model="item.wireStateTime" border="bottom"
					clearable></u--input>
				<u--input placeholder="综合布线状态" v-model="item.wireState" border="bottom" clearable></u--input>
				<u--input placeholder="弱点端接状态" v-model="item.eleState" border="bottom" clearable></u--input>
				<u-radio-group class="radio" v-model="item.eqIsPresent" placement="row">
					机柜到场状态：
					<u-radio label="已到场" name="已到场" shape="circle"></u-radio>
					<u-radio label="未到场" name="未到场" shape="circle"></u-radio>
				</u-radio-group>
				<u--input placeholder="签单状态" v-model="item.signState" border="bottom" clearable></u--input>
			</view>

			<view class="card-public">
				<text>现场验收图片：</text>
				<u-upload :fileList="fileList2" @afterRead="afterRead" @delete="deletePic" name="2" multiple
					:maxCount="10"></u-upload>
			</view>

			<view class=" card-public">
				<text>附件</text>
				<view class="btn">
					<u-button type="primary" @click="downLoad1" :plain="true" text="导出PDF图纸"></u-button>
					<u-button type="primary" @click="downLoad2" :plain="true" text="导出设备清单"></u-button>
				</view>

			</view>

		</view>
		<view class="sub">
			<u-button type="primary" @click="save" text="保存草稿"></u-button>
			<u-button type="primary" @click="sub" text="提交"></u-button>
		</view>
	</view>
</template>

<script>
	import request from '../../utils/request.js'
	export default {
		data() {
			return {
				p1: false,
				p2: false,
				p3: false,
				item: {
					isGet: null, //是否收到货物
					arriveTime: null, //确认进场时间
					isOptionalDate: null, //核对点位时间
					wireStateTime: null, //综合布线状态 时间
					wireState: null, //综合布线状态
					eleState: null, //弱电端接状态
					eqIsPresent: null, //机柜到场日期 是否到场
					eqIsOptionalTime: null, //机柜到场日期 时间
					signState: null, //签单状态

					serialNumber: null, //序列号
					faultEquipment: null, //MA故障设备

					faultClass: null, //MA故障分类	
					faultCode: null, //MA故障代码	
					soluteCode: null, //MA解决代码	
					repairType: null, //MA解决类型

					workOrderId: null,
					finishTime: null,
					workOrderStatus: 20, //工单状态
					workOrderTypeId: null //工单类型

				},
				workOrderImagesBos: [],
				detailInfo: {},
				fileList1: [],
				fileList2: [],
				value: null,
				show: {
					show1: false,
					show2: false,
					show3: false,
					show4: false,
				},
				//MA故障分类
				faultClassName: this.$store.state.mafa.ma_fault_class,
				//MA故障代码
				faultCode: this.$store.state.mafa.ma_fault_code,
				//MA解决代码
				soluteCode: this.$store.state.mafa.ma_solute_code,
				//MA解决类型
				repairType: this.$store.state.mafa.repair_type //remote Onsite
			};
		},
		computed: {
			remarktitle() {
				switch (this.detailInfo.workOrderTypeId) {
					case 1:
						return "派单备注"
					case 5:
						return "申请转单备注"
					case 10:
						return "拒绝转单理由"
					case 15:
						return "审核驳回理由"
					case 13:
						return "工程师现场反馈"
					case 20:
						return "项目经理备注"
					default:
						return "备注字典找不到"
				}
			},
			workid() {
				return this.detailInfo.workOrderId[0] + this.detailInfo.workOrderId[1] + this.detailInfo.workOrderId[2]
			},

		},
		beforeUpdate() {
			this.p1 = this.workid == "KMa" || this.workid == "KCa" || this.workid == "KMl" || this.workid == "KMr"
			this.p2 = this.workid == "KMa" || this.workid == "KMl" || this.workid == "KMr"
			this.p3 = this.workid == "KOa"
		},
		onLoad(option) {
			this.mountSelect(option.workOrderId)
			uni.getStorage({
				key: option.workOrderId, // 储存在本地的变量名
				success: res => {
					// 成功后的回调
					this.item = res.data // hello  这里可做赋值的操作 
				}
			})
			uni.getStorage({
				key: option.workOrderId + "img", // 储存在本地的变量名
				success: res => {
					// 成功后的回调
					this.workOrderImagesBos = res.data // hello  这里可做赋值的操作
				}
			})
			uni.getStorage({
				key: option.workOrderId + "filelist1", // 储存在本地的变量名
				success: res => {
					// 成功后的回调
					this.fileList1 = res.data // hello  这里可做赋值的操作 
				}
			})
			uni.getStorage({
				key: option.workOrderId + "filelist2", // 储存在本地的变量名
				success: res => {
					// 成功后的回调
					this.fileList2 = res.data // hello  这里可做赋值的操作
				}
			})


			// uni.getStorageSync(option.workOrderId)
			// uni.removeStorageSync('item');
		},
		methods: {
			gettime() {
				var nowdate = new Date();
				var year = nowdate.getFullYear()
				var month = nowdate.getMonth() + 1
				var date = nowdate.getDate()
				var day = nowdate.getDay()
				var h = nowdate.getHours()
				var m = nowdate.getMinutes()
				var s = nowdate.getSeconds()
				var nowtime = year + "-" + month + "-" + date + " " + h + ":" + m + ":" + s;
				return nowtime
			},
			downLoad1() {
				this.downImages("导出PDF图纸")
			},
			downLoad2() {
				this.downImages("导出设备清单")
			},
			confirm(e, id) {
				if (id == 1) {
					if (e.value[0] == "其他") {
						this.item.faultClass = "100"
						this.closeSelect()
					} else {
						this.item.faultClass = e.indexs[0].toString()
						this.closeSelect()
					}

				} else if (id == 2) {
					this.item.faultCode = e.indexs[0].toString()
					this.closeSelect()
				} else if (id == 3) {
					this.item.soluteCode = e.indexs[0].toString()
					this.closeSelect()
				} else if (id == 4) {
					if (e.value[0] == "远程") {
						this.item.repairType = "remote"
						this.closeSelect()
					} else if (e.value[0] == "上门") {
						this.item.repairType = "Onsite"
						this.closeSelect()
					}

				}
				console.log('confirm', e)
			},
			closeSelect() {
				this.show.show1 = false
				this.show.show2 = false
				this.show.show3 = false
				this.show.show4 = false
			},
			cancel() {

			},
			//获取核对点位图片
			// getImglist1(data){
			// 	this.fileList1 = data.filter(item => {
			// 		let dir = {}
			// 		if(item.imageTypeId == "15") {
			// 			dir.url = item.imagePath
			// 		}
			// 		return dir
			// 	})
			// 	this.fileList2 = data.filter(item => {
			// 		let dir = {}
			// 		if(item.imageTypeId == "15") {
			// 			dir.url = item.imagePath
			// 		}
			// 		return dir
			// 	})	
			// },
			//清理本地缓存数据
			clearLocal() {
				uni.removeStorageSync(this.detailInfo.workOrderId)
				uni.removeStorageSync(this.detailInfo.workOrderId + "img")
				uni.removeStorageSync(this.detailInfo.workOrderId + "filelist1")
				uni.removeStorageSync(this.detailInfo.workOrderId + "filelist2")
			},
			//提交
			sub() {
				this.item.workOrderId = this.detailInfo.workOrderId
				this.item.finishTime = this.gettime()
				var obj = this.item
				for (var key in obj) {
					if (obj[key] == null) {
						delete obj[key]
					}
				}
				this.item = obj
				if (this.workid == "KOa") {
					this.adopen()
				} else if (this.workid == "KOl") {
					this.lvopen()
				} else if (this.workid == "KCa") {
					this.adclose()
				} else if (this.workid == "KCl") {
					this.lvclose()
				} else if (this.workid == "KMa") {
					this.adma()
				} else if (this.workid == "KMl") {
					this.lvma()
				} else if (this.workid == "KOO") {
					this.koo()
				} else if (this.workid == "KMr") {
					this.rgma()
				}
			},
			//保存草稿
			save() {
				uni.setStorage({
					key: this.detailInfo.workOrderId, // 存储值的名称
					data: this.item, //   将要存储的数据
					success: res => {
						// 成功后的回调
					}
				});
				uni.setStorage({
					key: this.detailInfo.workOrderId + "img", // 存储值的名称
					data: this.workOrderImagesBos, //   将要存储的数据
					success: res => {
						// 成功后的回调
					}
				});
				uni.setStorage({
					key: this.detailInfo.workOrderId + "filelist1", // 存储值的名称
					data: this.fileList1, //   将要存储的数据
					success: res => {
						// 成功后的回调
					}
				});
				uni.setStorage({
					key: this.detailInfo.workOrderId + "filelist2", // 存储值的名称
					data: this.fileList2, //   将要存储的数据
					success: res => {
						// 成功后的回调
					}
				});
				uni.switchTab({
					url: '/pages/home/index'
				})


			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url, event.name)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url, name) {
				return new Promise((resolve, reject) => {
					uni.$u.http.upload('https://system-test.kfn.com.cn/prod-api/system/oss/upload', {
						params: {},
						/* 会加在url上 */
						// #ifdef APP-PLUS || H5
						files: [], // 需要上传的文件列表。使用 files 时，filePath 和 name 不生效。App、H5（ 2.6.15+）
						// #endif
						filePath: url, // 要上传文件资源的路径。
						// 注：如果局部custom与全局custom有同名属性，则后面的属性会覆盖前面的属性，相当于Object.assign(全局，局部)
						custom: {
							auth: true
						}, // 可以加一些自定义参数，在拦截器等地方使用。比如这里我加了一个auth，可在拦截器里拿到，如果true就传token
						name: 'file', // 文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容
						// #ifdef H5 || APP-PLUS
						timeout: 60000, // H5(HBuilderX 2.9.9+)、APP(HBuilderX 2.9.9+)
						// #endif
						header: {
							Authorization: uni.getStorageSync("token")
						},
						/* 会与全局header合并，如有同名属性，局部覆盖全局 */
						formData: {}, // HTTP 请求中其他额外的 form data
						// 返回当前请求的task, options。请勿在此处修改options。非必填
						getTask: (task, options) => {},
					}).then((res) => {
						if (name == 1) {
							this.workOrderImagesBos.push({
								imagePath: res.data.data.url.replace("blob:", ""),
								imageTypeId: "5",
								workOrderId: this.detailInfo.workOrderId
							})
						} else if (name == 2) {
							this.workOrderImagesBos.push({
								imagePath: res.data.data.url.replace("blob:", ""),
								imageTypeId: "10",
								workOrderId: this.detailInfo.workOrderId
							})
						}
						resolve(res.data.data.url)
						// 返回的res.data 已经进行JSON.parse
					}).catch(err => {

					})


					// let a = uni.uploadFile({
					// 	url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
					// 	filePath: url,
					// 	name: 'file',
					// 	formData: {
					// 		user: 'test'
					// 	},
					// 	success: (res) => {
					// 		setTimeout(() => {
					// 			resolve(res.data.data)
					// 		}, 1000)
					// 	}
					// });
				})
			},

			//请求有关
			mountSelect(workOrderId) {
				if (workOrderId[2] == "a") {
					request({
						url: "prod-api/system/ad-workOrder/" + workOrderId,
						method: "get",
					}).then((res) => {
						if (res.code < 300) {
							// this.getImgList(res.data.workOrderImagesVos)
							console.log(workOrderId)
							if (workOrderId[1] === "O") {
								this.detailInfo = res.data.selectAdOpenWorkOrderVo
							} else if (workOrderId[1] === "C") {
								this.detailInfo = res.data.selectAdCloseWorkOrderVo
							} else if (workOrderId[1] === "M") {
								this.detailInfo = res.data.selectAdMaWorkOrderVo
							}
						}
					})
				} else if (workOrderId[2] == "l") {
					request({
						url: "prod-api/system/lv-workOrder/" + workOrderId,
						method: "get",
					}).then((res) => {
						if (res.code < 300) {
							this.detailInfo = res.data.selectLvMaWorkOrderVo
						}
					})
				} else if (workOrderId[2] == "O") {
					request({
						url: "prod-api/system/workOrder/" + workOrderId,
						method: "get",
					}).then((res) => {
						if (res.code < 300) {
							this.detailInfo = res.data.otherWorkOrderVo
						}
					})
				} else if (workOrderId[2] == "r") {
					request({
						url: "prod-api/system/rg-workOrder/" + workOrderId,
						method: "get",
					}).then((res) => {
						if (res.code < 300) {
							this.detailInfo = res.data.selectRgMaWorkOrderVo

						}
						console.log(res)
					})
				}

			},
			//导出订单图片
			downImages(type) {
				// if(type=="导出PDF图纸"){
				// 	var paramsData={
				// 		imageTypeId: "20",
				// 		workOrderId: this.detailInfo.workOrderId
				// 	}
				// }else if(type=="导出设备清单"){
				// 	var paramsData={
				// 		imageTypeId: "25",
				// 		workOrderId: this.detailInfo.workOrderId
				// 	}
				// }
				request({
					url: "prod-api/system/orderImages/downloadList?imagesBos=" + this.detailInfo.workOrderId,
					method: "get",
				}).then((res) => {
					console.log(res)
					if (res.code < 300) {
						uni.downloadFile({
							url: res.data,
							success: (res) => {
								if (res.statusCode === 200) {
									uni.saveImageToPhotosAlbum({
										filePath: res.tempFilePath,
										success: function() {
											uni.showToast({
												title: "保存成功",
												icon: "none"
											});
										},
										fail: function() {
											uni.showToast({
												title: "保存失败，请稍后重试",
												icon: "none"
											});
										}
									});
								}
							}
						})
					}
				})
			},

			adma() {
				this.item.workOrderTypeId = 1
				console.log(this.item)
				console.log(this.workOrderImagesBos)
				request({
					url: "prod-api/system/ad-workOrder",
					method: "put",
					data: {
						updateAdMaWorkOrderBo: this.item,
						workOrderImagesBos: this.workOrderImagesBos
					}
				}).then((res) => {
					console.log(res)
					if (res.code < 300) {
						this.clearLocal()
						uni.switchTab({
							url: '/pages/home/index'
						})
					}
				})
			},
			adopen() {
				this.item.workOrderTypeId = 0
				console.log(this.item)
				console.log(this.workOrderImagesBos)
				request({
					url: "prod-api/system/ad-workOrder",
					method: "put",
					data: {
						updateAdOpenWorkOrderBo: this.item,
						workOrderImagesBos: this.workOrderImagesBos
					}
				}).then((res) => {
					console.log(res)
					if (res.code < 300) {
						this.clearLocal()
						uni.switchTab({
							url: '/pages/home/index'
						})
					}
				})
			},
			adclose() {
				this.item.workOrderTypeId = 2
				request({
					url: "prod-api/system/ad-workOrder",
					method: "put",
					data: {
						updateAdCloseWorkOrderBo: this.item,
						workOrderImagesBos: this.workOrderImagesBos
					}
				}).then((res) => {
					console.log(res)
					if (res.code < 300) {
						this.clearLocal()
						uni.switchTab({
							url: '/pages/home/index'
						})
					}
				})
			},
			lvma() {
				this.item.workOrderTypeId = 1
				console.log(this.item)
				console.log(this.workOrderImagesBos)
				request({
					url: "prod-api/system/lv-workOrder",
					method: "put",
					data: {
						updateLvMaWorkOrderBo: this.item,
						workOrderImagesBos: this.workOrderImagesBos
					}
				}).then((res) => {
					console.log(res)
					if (res.code < 300) {
						this.clearLocal()
						uni.switchTab({
							url: '/pages/home/index'
						})
					}
				})
			},
			koo() {
				console.log(this.item)
				console.log(this.workOrderImagesBos)
				this.item.workOrderImagesBos = this.workOrderImagesBos
				request({
					url: "prod-api/system/workOrder",
					method: "put",
					data: this.item
				}).then((res) => {
					console.log(res)
					if (res.code < 300) {
						this.clearLocal()
						uni.switchTab({
							url: '/pages/home/index'
						})
					}
				})
			},
			rgma() {
				this.item.workOrderTypeId = 1
				console.log(this.item)
				console.log(this.workOrderImagesBos)
				request({
					url: "prod-api/system/rg-workOrder",
					method: "put",
					data: {
						updateRgMaWorkOrderBo: this.item,
						workOrderImagesBos: this.workOrderImagesBos
					}
				}).then((res) => {
					console.log(res)
					if (res.code < 300) {
						this.clearLocal()
						uni.switchTab({
							url: '/pages/home/index'
						})
					}
				})
			},
			// lvopen(){
			// 	this.item.workOrderTypeId=1
			// 	request({
			// 		url: "prod-api/system/lv-workOrder",
			// 		method: "put",
			// 		data:{
			// 			updateLvOpenWorkOrderBo:this.item,
			// 			workOrderImagesBos:this.workOrderImagesBos
			// 		}
			// 	}).then((res)=>{
			// 		if(res.code<300){	
			// 			uni.switchTab({
			//			    url: '/pages/home/index'
			//							})
			// 			}
			// 		})
			// },
			// lvclose(){
			// 	request({
			// 		url: "prod-api/system/lv-workOrder",
			// 		method: "put",
			// 		data:{
			// 			updateRgCloseWorkOrderBo:this.item,
			// 			workOrderImagesBos:this.workOrderImagesBos
			// 		}
			// 	}).then((res)=>{
			// 		if(res.code<300){	
			// 			uni.switchTab({
			//			    url: '/pages/home/index'
			//			})
			// 			}
			// 		})
			// },


		}
	}
</script>

<style lang="scss">
	.contain {
		width: 100%;
		height: 100%;
		position: absolute;
		background-color: rgb(245, 245, 245);

		.card-contain {
			.card-public {
				display: flex;
				flex-direction: column;
				background-color: white;
				justify-content: space-between;
				width: 90%;
				margin: 30upx 5%;
				box-sizing: border-box;
				padding: 10upx 0;
				padding-left: 40upx;
				border-radius: 30upx 30upx 30upx 30upx;
				box-shadow: 0 0 15px rgb(232, 232, 232);

				.textline {
					border-bottom: 1px solid rgb(218, 219, 222);
					color: rgb(192, 196, 204);
					width: 100%;
					line-height: 60upx;
				}

				.radio {
					margin-top: 20upx;
				}

				text {
					font-size: 32upx;
					margin-top: 8upx;
					margin-bottom: 20upx;
				}
			}

			.btn {
				display: flex;
			}

		}

		.sub {
			display: flex;
			justify-content: space-between;

			button {
				width: 300upx;
				height: 60upx;
				margin-bottom: 50upx;
			}

		}
	}
</style>
